//
//  CYGetiVC.m
//  CreditAddressBook
//
//  Created by Mosu&Cuu on 16/6/13.
//  Copyright © 2016年 Lee. All rights reserved.
//

#import "CYGetiVC.h"
#import "LEUnitView+LE.h"
#import "CYCreditTool.h"
#import "CustomIOSAlertView.h"
#import "UIImageView+WebCache.h"
#import "MWPhotoBrowser.h"
#import "LEPhotoBrowserTool.h"
#import "CYLoginHttpTool.h"
#import "CYUser.h"
#import "CYJsonInfo.h"
#import "NSObject+MJKeyValue.h"
#import "LEImagePickerTool.h"
#import "MBProgressHUD+LE.h"
#import "CYDiscoveryHttpTool.h"
#import "UIButton+WebCache.h"

@interface CYGetiVC ()
@property (weak, nonatomic) IBOutlet UIView *baipaiSubmitView;
@property (weak, nonatomic) IBOutlet UIView *shouchiSubmitView;
@property (weak, nonatomic) IBOutlet UIView *mentouSubmitView;
@property (weak, nonatomic) IBOutlet UIView *changsuoSubmitView;
@property (weak, nonatomic) IBOutlet UILabel *baipaistatueLab;
@property (weak, nonatomic) IBOutlet UILabel *shouchistatueLab;
@property (weak, nonatomic) IBOutlet UILabel *mentoustatueLab;
@property (weak, nonatomic) IBOutlet UILabel *huanjingstatueLab;

@property (nonatomic, strong) NSMutableArray *baipaiUrl;
@property (nonatomic, strong) NSMutableArray *shouchiUrl;
@property (nonatomic, strong) NSMutableArray *mentouUrl;
@property (nonatomic, strong) NSMutableArray *huanjingUrl;

@property (nonatomic, strong) NSMutableDictionary *getiDic;

@property (nonatomic, strong) NSArray *baipaiArr;
@property (nonatomic, strong) NSArray *shouchiArr;
@property (nonatomic, strong) NSArray *mentouArr;
@property (nonatomic, strong) NSArray *huanjingArr;

@property (weak, nonatomic) IBOutlet UIView *getirecordView;
@property (weak, nonatomic) IBOutlet UILabel *getirecordLab;
@property (nonatomic, strong) NSMutableArray *recordimageUrls;
@property (weak, nonatomic) IBOutlet UIButton *zhizhaobaipai;
@property (weak, nonatomic) IBOutlet UIButton *shouchizhizhaobaipai;
@property (weak, nonatomic) IBOutlet UIButton *shouchizhizhaobaipai2;
@property (weak, nonatomic) IBOutlet UIButton *mentou1;
@property (weak, nonatomic) IBOutlet UIButton *huanjing1;


@end

@implementation CYGetiVC

- (void)viewDidLoad {
     [super viewDidLoad];
     self.title = @"个体私企";
     
     [_zhizhaobaipai sd_setImageWithURL:[NSURL URLWithString:@"http://www.credit-manage.com:8058/apppic/siqi/1.png"] forState:UIControlStateNormal];
     [_shouchizhizhaobaipai sd_setImageWithURL:[NSURL URLWithString:@"http://www.credit-manage.com:8058/apppic/siqi/2.jpg"] forState:UIControlStateNormal];
     [_shouchizhizhaobaipai2 sd_setImageWithURL:[NSURL URLWithString:@"http://www.credit-manage.com:8058/apppic/siqi/3.jpg"] forState:UIControlStateNormal];
     [_mentou1 sd_setImageWithURL:[NSURL URLWithString:@"http://www.credit-manage.com:8058/apppic/siqi/4.png"] forState:UIControlStateNormal];
     [_huanjing1 sd_setImageWithURL:[NSURL URLWithString:@"http://www.credit-manage.com:8058/apppic/siqi/5.png"] forState:UIControlStateNormal];





     
     _baipaiUrl = [[NSMutableArray alloc]init];
     _shouchiUrl = [[NSMutableArray alloc]init];
     _mentouUrl = [[NSMutableArray alloc]init];
     _huanjingUrl = [[NSMutableArray alloc]init];
     _getiDic = [[NSMutableDictionary alloc]init];
     _recordimageUrls = [[NSMutableArray alloc]init];
     
     
     NSDictionary *getidic = [[NSDictionary alloc] initWithContentsOfFile:@"getiImage.plist"];
     _baipaiArr = [getidic objectForKey:@"baipaiUrl"];
     _shouchiArr = [getidic objectForKey:@"shouchiUrl"];
     _mentouArr = [getidic objectForKey:@"mentouUrl"];
     _huanjingArr = [getidic objectForKey:@"huanjingUrl"];
     
     
     if (_baipaiArr.count) {
          _baipaistatueLab.text = @"已上传，再次上传将会重新更新此类";
          _baipaistatueLab.textColor = [UIColor redColor];
     }
     if (_shouchiArr.count) {
          _shouchistatueLab.text = @"已上传，再次上传将会重新更新此类";
          _shouchistatueLab.textColor = [UIColor redColor];
     }
     if (_huanjingArr.count) {
          _huanjingstatueLab.text = @"已上传，再次上传将会重新更新此类";
          _huanjingstatueLab.textColor = [UIColor redColor];
     }
     if (_mentouArr.count) {
          _mentoustatueLab.text = @"已上传，再次上传将会重新更新此类";
          _mentoustatueLab.textColor = [UIColor redColor];
     }
     
     
     UIButton *rightButton = [[UIButton alloc]initWithFrame:CGRectMake(0,0,50,30)];
     rightButton.titleLabel.textColor = [UIColor blackColor];
     [rightButton addTarget:self action:@selector(submitclick) forControlEvents:UIControlEventTouchUpInside];
     [rightButton setTitle:@"确定" forState:UIControlStateNormal];
     UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightButton];
     self.navigationItem.rightBarButtonItem= rightItem;

     
     [self setupUploadView];
     [self setrecordImageView];
    // Do any additional setup after loading the view from its nib.
}

- (void)setrecordImageView{
     
     // image 后缀
     NSString *preFixString = @"http://image.credit-manage.com/";
     // 屏幕的宽度 (也是承载imageView的view的宽度)
     CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width;
     // image各个边界的间隔
     CGFloat space = 10.f;
     // imageview的width (设定为5张)
     CGFloat imgWidth = (screenWidth - space * 6 ) / 5;
     // imageview的height
     CGFloat imgHeight = _getirecordView.frame.size.height - space * 2;
     // 由于是xib + 手码, 代码获取xib控件的frame 必须在主线程中获取
     dispatch_async(dispatch_get_main_queue(), ^{
          CYUser *user = [CYLoginHttpTool userData].user;
          [CYHttpTool postWithURL:[CYURL shared].getWorkInfoPics params:@{@"userId":user.userId,@"token":user.token} success:^(id json) {
               //        CYLog(@"json = %@",json);
               CYJsonInfo *jsoninf = [CYJsonInfo objectWithKeyValues:json];
               NSMutableArray *data = jsoninf.data;
               for (NSDictionary *dic in data) {
                    // 取网络图片地址
                    NSString *wokrPicAddress = [dic objectForKey:@"workPicAddress"];
                    //取type
                    NSInteger type = [[dic objectForKey:@"type"] integerValue];
                    switch (type) {
                         case 2:
                              [_recordimageUrls addObject:wokrPicAddress];
                              break;
                              
                         case 4:
                              [_recordimageUrls addObject:wokrPicAddress];
                              break;
                    }
               }
               
               for (int i = 0; i < _recordimageUrls.count; i++ ) {
                    // image的url string
                    NSString *imgUrlString = [_recordimageUrls objectAtIndex:i];
                    imgUrlString = [preFixString stringByAppendingString:imgUrlString];
                    // 创建imageView
                    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(space + (imgWidth + space) * i, space, imgWidth, imgHeight)];
                    imgView.backgroundColor = [UIColor whiteColor];
                    // 此处你可使用SDWebImage来加载
                    [imgView sd_setImageWithURL:[NSURL URLWithString:imgUrlString]];
                    [_getirecordView addSubview:imgView];
               }
               if (_recordimageUrls.count) {
                    _getirecordLab.hidden = YES;
                    UITapGestureRecognizer *tag = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(photoBrowser:)];
                    [_getirecordView addGestureRecognizer:tag];
               }
               
          } failure:^(NSError *error) {
               
          }];
     });
}
/**
 *  图片浏览器
 */
- (void)photoBrowser:(UITapGestureRecognizer *)tag {
     NSArray *urls = _recordimageUrls;
     MWPhoto *photo = nil;
     NSMutableArray *photos = [NSMutableArray array];
     for (NSString *url in urls) {
          photo = [MWPhoto photoWithURL:CYURLWithService(url)];
          //        photo.caption = @"title";
          [photos addObject:photo];
     }
     [LEPhotoBrowserTool showPhotoBrowserWithController:self photos:photos];
}



-(void)submitclick{
     [_getiDic removeAllObjects];
     if (_baipaiUrl.count) {
          [_getiDic setObject:_baipaiUrl forKey:@"baipaiUrl"];
          
     }else if (!_baipaiUrl.count && _baipaiArr.count) {
          [_getiDic setObject:_baipaiArr forKey:@"baipaiUrl"];
          
     }
     if (_shouchiUrl.count) {
          [_getiDic setObject:_shouchiUrl forKey:@"shouchiUrl"];
          
     }else if (!_shouchiUrl.count && _shouchiArr.count) {
          [_getiDic setObject:_shouchiArr forKey:@"shouchiUrl"];
          
     }
     if (_huanjingUrl.count) {
          [_getiDic setObject:_huanjingUrl forKey:@"huanjingUrl"];
          
     }
     else if (!_huanjingUrl.count && _huanjingArr.count) {
          [_getiDic setObject:_huanjingArr forKey:@"huanjingUrl"];
          
     }
     if (_mentouUrl.count) {
          [_getiDic setObject:_mentouUrl forKey:@"mentouUrl"];
          
     }
     else if (!_mentouUrl.count && _mentouArr.count) {
          [_getiDic setObject:_mentouArr forKey:@"mentouUrl"];
          
     }
     
     
     if (_baipaiArr.count != 0 && _shouchiArr.count != 0 && _mentouArr.count != 0 && _huanjingArr.count != 0 ) {
          
          NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
          NSString *plistPath1= [paths objectAtIndex:0];
          NSLog(@"%@",plistPath1);
          //得到完整的路径名
          NSString *fileName = [plistPath1 stringByAppendingPathComponent:@"getiImage.plist"];
          //NSMutableDictionary *myDic = [[NSMutableDictionary alloc]init];
          //[myDic setValuesForKeysWithDictionary:cityDic];
          NSFileManager *fm = [NSFileManager defaultManager];
          
          if ([fm createFileAtPath:fileName contents:nil attributes:nil] ==YES) {
               [_getiDic writeToFile:fileName atomically:YES];
               NSLog(@"文件写入完成");
               CYLog(@"getiDic = %@",_getiDic);
               [[NSNotificationCenter defaultCenter] postNotificationName:@"CYNewWorkNotification" object:nil userInfo:nil];
               [self.navigationController popViewControllerAnimated:YES];
               
          }
          return;
     }
     
     
     if (_baipaiUrl.count != 0 && _shouchiUrl.count != 0 && _mentouUrl.count != 0 && _huanjingUrl.count != 0) {
          
          NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
          NSString *plistPath1= [paths objectAtIndex:0];
          NSLog(@"%@",plistPath1);
          //得到完整的路径名
          NSString *fileName = [plistPath1 stringByAppendingPathComponent:@"getiImage.plist"];
          //NSMutableDictionary *myDic = [[NSMutableDictionary alloc]init];
          //[myDic setValuesForKeysWithDictionary:cityDic];
          NSFileManager *fm = [NSFileManager defaultManager];
          
          if ([fm createFileAtPath:fileName contents:nil attributes:nil] ==YES) {
               [_getiDic writeToFile:fileName atomically:YES];
               NSLog(@"文件写入完成");
               CYLog(@"getiDic = %@",_getiDic);
               [[NSNotificationCenter defaultCenter] postNotificationName:@"CYNewWorkNotification" object:nil userInfo:nil];
               [self.navigationController popViewControllerAnimated:YES];
               
          }
     }else{
          CYAlertView(@"请按照要求提交！");
          
     }

     
}

- (void)setupUploadView {
     //摆拍
     [LEUnitView imageScrollView:self.baipaiSubmitView controller:self block:^(LEUnitView *unitView, LEUnitCell *cell, UIImage *image) {
          [CYCreditTool uploadPicImage:image success:^(NSString *imageUrl) {
          [_baipaiUrl addObject:imageUrl];
               cell.ratio = 1;
          } failure:^(NSError *error) {
               //删除
               [unitView deleteLastCell];
          } uploadProgress:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
               CGFloat a = totalBytesWritten;
               CGFloat b = totalBytesExpectedToWrite;
               cell.ratio = a/b - .01;
          }];
     } cellClick:^(LEUnitView *unitView, LEUnitCell *cell) {
     }];
     //手持
     [LEUnitView imageScrollView:self.shouchiSubmitView controller:self block:^(LEUnitView *unitView, LEUnitCell *cell, UIImage *image) {
          [CYCreditTool uploadPicImage:image success:^(NSString *imageUrl) {
            [_shouchiUrl addObject:imageUrl];
               cell.ratio = 1;
          } failure:^(NSError *error) {
               //删除
               [unitView deleteLastCell];
          } uploadProgress:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
               CGFloat a = totalBytesWritten;
               CGFloat b = totalBytesExpectedToWrite;
               cell.ratio = a/b - .01;
          }];
     } cellClick:^(LEUnitView *unitView, LEUnitCell *cell) {
     }];
     //门头
     [LEUnitView imageScrollView:self.mentouSubmitView controller:self block:^(LEUnitView *unitView, LEUnitCell *cell, UIImage *image) {
          [CYCreditTool uploadPicImage:image success:^(NSString *imageUrl) {
             [_mentouUrl addObject:imageUrl];
               cell.ratio = 1;
          } failure:^(NSError *error) {
               //删除
               [unitView deleteLastCell];
          } uploadProgress:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
               CGFloat a = totalBytesWritten;
               CGFloat b = totalBytesExpectedToWrite;
               cell.ratio = a/b - .01;
          }];
     } cellClick:^(LEUnitView *unitView, LEUnitCell *cell) {
     }];
     //环境
     [LEUnitView imageScrollView:self.changsuoSubmitView controller:self block:^(LEUnitView *unitView, LEUnitCell *cell, UIImage *image) {
          [CYCreditTool uploadPicImage:image success:^(NSString *imageUrl) {
             [_huanjingUrl addObject:imageUrl];
               cell.ratio = 1;
          } failure:^(NSError *error) {
               //删除
               [unitView deleteLastCell];
          } uploadProgress:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
               CGFloat a = totalBytesWritten;
               CGFloat b = totalBytesExpectedToWrite;
               cell.ratio = a/b - .01;
          }];
     } cellClick:^(LEUnitView *unitView, LEUnitCell *cell) {
     }];
     
}

- (IBAction)baipai1:(id)sender {
     UIImageView *shareView = [[UIImageView alloc] init];
     [shareView sd_setImageWithURL:[NSURL URLWithString:@"http://www.credit-manage.com:8058/apppic/siqi/1.png"]];
     //    CGFloat ratio = shareView.height / shareView.width;
     //    CGFloat height = CYScreeSize.height - 130;
     CustomIOSAlertView *alertView = [[CustomIOSAlertView alloc] init];
     CGFloat w = CYScreeSize.width - 32;
     shareView.frame = CGRectMake(0, 0, w, self.view.height-160);
     alertView.containerView = shareView;
     alertView.buttonTitles = @[@"知道了"];
     
     [alertView setOnButtonTouchUpInside:^(CustomIOSAlertView *alertView, int buttonIndex) {
     }];
     [alertView show];
}
- (IBAction)baipai2:(id)sender {
}
- (IBAction)baipai3:(id)sender {
}
- (IBAction)baipai4:(id)sender {
}
- (IBAction)shouchi1:(id)sender {
     UIImageView *shareView = [[UIImageView alloc] init];
     [shareView sd_setImageWithURL:[NSURL URLWithString:@"http://www.credit-manage.com:8058/apppic/siqi/2.jpg"]];
     //    CGFloat ratio = shareView.height / shareView.width;
     //    CGFloat height = CYScreeSize.height - 130;
     CustomIOSAlertView *alertView = [[CustomIOSAlertView alloc] init];
     CGFloat w = CYScreeSize.width - 32;
     shareView.frame = CGRectMake(0, 0, w, self.view.height-160);
     alertView.containerView = shareView;
     alertView.buttonTitles = @[@"知道了"];
     
     [alertView setOnButtonTouchUpInside:^(CustomIOSAlertView *alertView, int buttonIndex) {
     }];
     [alertView show];

}
- (IBAction)shouchi2:(id)sender {
     UIImageView *shareView = [[UIImageView alloc] init];
     [shareView sd_setImageWithURL:[NSURL URLWithString:@"http://www.credit-manage.com:8058/apppic/siqi/3.jpg"]];
     //    CGFloat ratio = shareView.height / shareView.width;
     //    CGFloat height = CYScreeSize.height - 130;
     CustomIOSAlertView *alertView = [[CustomIOSAlertView alloc] init];
     CGFloat w = CYScreeSize.width - 32;
     shareView.frame = CGRectMake(0, 0, w, self.view.height-160);
     alertView.containerView = shareView;
     alertView.buttonTitles = @[@"知道了"];
     
     [alertView setOnButtonTouchUpInside:^(CustomIOSAlertView *alertView, int buttonIndex) {
     }];
     [alertView show];

}
- (IBAction)shouchi3:(id)sender {
}
- (IBAction)shouchi4:(id)sender {
}
- (IBAction)mentou1:(id)sender {
     UIImageView *shareView = [[UIImageView alloc] init];
     [shareView sd_setImageWithURL:[NSURL URLWithString:@"http://www.credit-manage.com:8058/apppic/siqi/4.png"]];
     //    CGFloat ratio = shareView.height / shareView.width;
     //    CGFloat height = CYScreeSize.height - 130;
     CustomIOSAlertView *alertView = [[CustomIOSAlertView alloc] init];
     CGFloat w = CYScreeSize.width - 32;
     shareView.frame = CGRectMake(0, 0, w, self.view.height-160);
     alertView.containerView = shareView;
     alertView.buttonTitles = @[@"知道了"];
     
     [alertView setOnButtonTouchUpInside:^(CustomIOSAlertView *alertView, int buttonIndex) {
     }];
     [alertView show];

}
- (IBAction)mentou2:(id)sender {
}
- (IBAction)mentou3:(id)sender {
}
- (IBAction)mentou4:(id)sender {
}
- (IBAction)changsuo1:(id)sender {
     UIImageView *shareView = [[UIImageView alloc] init];
     [shareView sd_setImageWithURL:[NSURL URLWithString:@"http://www.credit-manage.com:8058/apppic/siqi/5.png"]];
     //    CGFloat ratio = shareView.height / shareView.width;
     //    CGFloat height = CYScreeSize.height - 130;
     CustomIOSAlertView *alertView = [[CustomIOSAlertView alloc] init];
     CGFloat w = CYScreeSize.width - 32;
     shareView.frame = CGRectMake(0, 0, w, self.view.height-160);
     alertView.containerView = shareView;
     alertView.buttonTitles = @[@"知道了"];
     
     [alertView setOnButtonTouchUpInside:^(CustomIOSAlertView *alertView, int buttonIndex) {
     }];
     [alertView show];

}
- (IBAction)changsuo2:(id)sender {
}
- (IBAction)changsuo3:(id)sender {
}
- (IBAction)changsuo4:(id)sender {
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
